using UnityEngine;
using UnityEngine.UI;

public class UnityAndroidConnector : MonoBehaviour
{
    [SerializeField]
    private Text text;

    /// <summary>
    /// 调用 Android Toast提示
    /// </summary>
    public void Toast()
    {
        SendMsgToAndroid("toast", "Hello Toast");
    }

    /// <summary>
    /// 调用 Android 获取Android字符串
    /// </summary>
    public void GetString()
    {
        SendMsgToAndroid("getString", "Hello Unity");
    }

    /// <summary>
    /// 发送消息给Android
    /// </summary>
    /// <param name="key"></param>
    /// <param name="msg"></param>
    public void SendMsgToAndroid(string key, string msg)
    {
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity  = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        currentActivity.Call("receiveUnityMsg", key, msg);
    }

    /// <summary>
    /// 接收来自 Android 端来的消息
    /// </summary>
    /// <param name="msg"></param>
    public void ReceiveAndroidMsg(string msg)
    {
        text.text = $"来自Android的消息:{msg}";
    }
}